<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }"><i class="el-icon-menu"></i> 系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-button type="primary" icon="delete" class="handle-del mr10" @click="add">新增</el-button>
            <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
        </div>
        <el-table :data="menuList" border style="width: 100%" ref="multipleTable"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="45"></el-table-column>
            <el-table-column prop="code" label="编号" sortable>
            </el-table-column>
            <el-table-column prop="name" label="名称">
            </el-table-column>
            <el-table-column prop="pcode" label="父编号" sortable>
            </el-table-column>
            <el-table-column prop="url" label="请求地址">
            </el-table-column>
            <el-table-column prop="num" label="排序" sortable>
            </el-table-column>
            <el-table-column prop="levels" label="层级" sortable>
            </el-table-column>
            <el-table-column prop="ismenu" label="是否是菜单">
                <template scope="scope">
                    <span v-if="scope.row.ismenu==1">是</span>
                    <span v-if="scope.row.ismenu==0">否</span>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template scope="scope">
                    <span v-if="scope.row.status==1">启用</span>
                    <span v-if="scope.row.status==0">禁用</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                        <el-button size="mini" type="primary"
                                   @click="handleEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="primary"
                                   @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                @current-change="getData"
                :page-size="size"
                layout="total, prev, pager, next"
                :total="total">
            </el-pagination>
        </div>

        <div id="dialog">
            <!--新增弹框-->
            <el-dialog title="新增" :visible.sync="addFormVisible">
                <el-form :model="addForm" :inline="true" :rules="rules" ref="addForm" label-position="right"
                         class="demo-form-inline">
                    <el-form-item prop="code" label="编号：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.code" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="name" label="名称：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="pcode" label="父编号：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.pcode" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="url" label="请求地址：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.url" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="num" label="排序：" :label-width="formLabeWidth">
                        <el-input v-model.number="addForm.num" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="ismenu" label="是否是菜单：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.ismenu" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="status" label="状态：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.status" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="icon" label="图标：" :label-width="formLabeWidth">
                        <el-input v-model="addForm.icon" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="addFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addSubmitForm('addForm')">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <div>
            <!--编辑弹框-->
            <el-dialog title="编辑" :visible.sync="editFormVisible">
                <el-form :model="editForm" :inline="true" :rules="rules" ref="editForm" label-position="right"
                         class="demo-form-inline">
                    <el-form-item prop="code" required label="编号：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.code" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="name" label="名称：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="pcode" label="父编号：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.pcode" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="url" label="请求地址：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.url" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="num" label="排序：" :label-width="formLabeWidth">
                        <el-input v-model.number="editForm.num" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="ismenu" label="是否是菜单：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.ismenu" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="status" label="状态：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.status" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item prop="icon" label="图标：" :label-width="formLabeWidth">
                        <el-input v-model="editForm.icon" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editSubmitForm('editForm')">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                menuList: [],
                addFormVisible: false,
                editFormVisible: false,
                addForm: {
                    code: "",
                    name: "",
                    pcode: "",
                    url: "",
                    num: "",
                    levels: "",
                    ismenu: "",
                    status: "",
                    icon: ""
                },
                editForm: {
                    code: "",
                    name: "",
                    pcode: "",
                    url: "",
                    num: "",
                    levels: "",
                    ismenu: "",
                    status: "",
                    icon: ""
                },
                formLabeWidth: '100px',
                rules: {
                    code: [
                        {required: true, message: '请输入编号', trigger: 'change'},
                    ],
                    name: [
                        {required: true, message: '请输入名称', trigger: 'change'}
                    ],
                    pcode: [
                        {required: true, message: '请选择上级编码', trigger: 'change'}
                    ],
                    url: [
                        {required: true, message: '请输入请求地址', trigger: 'change'}
                    ],
                    num: [
                        {type:'number', required: true, message: '请输入排序', trigger: 'change'}
                    ],
                    ismenu: [
                        {required: true, message: '请选择是否是菜单', trigger: 'change'}
                    ],
                    status: [
                        {required: true, message: '请选择是否启用', trigger: 'change'}
                    ]
                },
                total:0 ,
                currentPage: 1,
                size:10
            }
        },
        created() {
            this.getData();
        },
        methods: {
            //获取user数据
            getData(data) {
                let self = this;
                var Vstart=0;
                var Vsize=self.size;
                if(data!=undefined&&!isNaN(data)){
                    Vstart=data-1;
                }
                self.$axios.post("/api/menu/get",{pageNumber:Vstart,pageSize:self.size}).then((res) => {
                    self.menuList = res.data.data.content;
                    self.total=res.data.data.totalElements;
                })
            },
            //新增面板
            add: function () {
                this.addFormVisible = true;
            },
            //添加菜单
            addSubmitForm(formName) {
                const self = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        self.$axios.post('/api/menu/add', self.addForm)
                            .then(function (response) {
                                console.log(response);
                                self.$message({
                                    message: "添加成功",
                                    type: 'warning'
                                });
                                self.addFormVisible = false;
                                self.getData();
                            })
                            .catch(function (error) {
                                if (error.response) {
                                    self.$message({
                                        message: error.response.data.msg,
                                        type: 'error'
                                    });
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //编辑面板
            handleEdit(index, row) {
                const self = this;
                console.log(row);
                //self.$message('编辑第'+(index+1)+'行');
                self.editForm = row;
                self.editFormVisible = true;
            },
            //修改菜单
            editSubmitForm(formName) {
                const self = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        self.$axios.put('/api/menu/update', self.editForm)
                            .then(function (response) {
                                console.log(response);
                                self.$message({
                                    message: "修改成功",
                                    type: 'warning'
                                });
                                self.editFormVisible = false;
                                self.getData();
                            })
                            .catch(function (error) {
                                if (error.response) {
                                    self.$message({
                                        message: error.response.data.msg,
                                        type: 'error'
                                    });
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //删除菜单
            handleDelete(index, row) {
                const self = this;
                console.log(row.id);
                self.$confirm('确认删除吗？', '提示', {
                    type: 'warning'
                }).then(function () {
                    self.$axios.delete('/api/menu/delete/' + row.id)
                        .then(function (response) {
                            console.log(response);
                            self.$message({
                                message: "删除成功",
                                type: 'warning'
                            });
                            self.getData();
                        })
                        .catch(function (error) {
                            if (error.response) {
                                self.$message({
                                    message: error.response.data.msg,
                                    type: 'error'
                                });
                            }
                        })
                })
            },
            delAll() {
                const self = this,
                    length = self.multipleSelection.length;
                let str = '';
                self.del_list = self.del_list.concat(self.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += self.multipleSelection[i].name + ' ';
                }
                self.$message.error('删除了' + str);
                self.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
</style>
